package LearnAlgorithm.f_字符串;

import java.util.HashMap;

public class f判断两串字符集相同 {
	public static void main(String[] args) {
		String aString = "abcdefg";
		String bString = "aabbccddeeffgg";
		System.out.println(checkCharKindsSameApi(aString, bString));
		String cString = "你好世界";
		String dString = "你好世界你好世界a";
		System.out.println(checkCharKindsSameApiMore(cString, dString));
	}
	
	/**
	 * O(N)
	 * 利用哈希表使用哈希映射
	 * @param aString
	 * @param bString
	 * @return
	 */
	public static boolean checkCharKindsSameApiMore(String aString, String bString) {
		HashMap<Character, Integer> helperHashMap = new HashMap<Character, Integer>();
		char temp = 0;
		for (int i = 0; i < aString.length(); i++) {
			temp = aString.charAt(i);
			if (helperHashMap.get(temp) == null) {
				helperHashMap.put(temp, 1);
			}
		}
		for (int i = 0; i < bString.length(); i++) {
			temp = bString.charAt(i);
			if (helperHashMap.get(temp) == null) {
				return false;
			}
		}
		return true;
	}
	
	/**
	 * O(N)
	 * 针对ascii
	 * @param aString
	 * @param bString
	 * @return
	 */
	public static boolean checkCharKindsSameApi(String aString, String bString) {
		int[] ascii = new int[128];
		char temp = 0;
		for (int i = 0; i < aString.length(); i++) {
			temp = aString.charAt(i);
			if (ascii[(int) temp] == 0) {
				ascii[(int) temp] = 1;
			}
		}
		for (int i = 0; i < bString.length(); i++) {
			temp = bString.charAt(i);
			if (ascii[(int) temp] == 0) {
				return false;
			}
		}
		return true;
	}
}
